$(document).ready(function(){
	jQuery.fn.redswap = function(options){
		var selectedItems=this;
		redsettings = jQuery.extend({
			 speed:"Medium",
			 opacity:0.7,
			 onafterswap:null,//onafterswap(dragObj,dropObj)
			 onpreswap:null, //onpreswap(dragObj,dropObj) return true: continue, return false cancel drag & drop
			 oninit:null//oninit(obj)
		  }, options);
		
		if(redsettings.oninit!=null){
		    for(var i = 0;i<selectedItems.length;i++){
		        selectedItems[i].index = i;
		        selectedItems[i].allItems = selectedItems;
		        redsettings.oninit(selectedItems[i]);
		    }
		}
		else{
		    for(var i = 0;i<selectedItems.length;i++){
		        selectedItems[i].index = i;
		        selectedItems[i].allItems = selectedItems;
		    }
		}
		var allowTodrag = true;
		$(selectedItems).mouseover(function(){
			$(selectedItems).disableSelection();
			$(selectedItems).droppable({
				drop: function(event, ui) {
				    if(!allowTodrag) return;
				    allowTodrag = false;
					dropindex=$(selectedItems).index(this);
					var dragposition=$(selectedItems[dragindex]).position();
					var dropposition=$(selectedItems[dropindex]).position();
					var dragsize = new Object();
					var dropsize = new Object();
					dragsize.width=$(selectedItems[dragindex]).width();
					dragsize.height=$(selectedItems[dragindex]).height();
					
					dropsize.width=$(selectedItems[dropindex]).width();
					dropsize.height=$(selectedItems[dropindex]).height();
					
					
					/*
					
					*/
					if(redsettings.onpreswap!=null){
					    if(!redsettings.onpreswap(selectedItems[dragindex],selectedItems[dropindex])){
					        allowTodrag = true;
					        return;
					    }
                    }
					withDifference=parseInt(dragposition.left)-parseInt(dropposition.left);
					heightDifference=parseInt(dragposition.top)-parseInt(dropposition.top);
					
					$(selectedItems[dropindex]).animate({
						left:'+='+withDifference,
						top:'+='+heightDifference,
						width: dragsize.width,
						height: dragsize.height
					},redsettings.speed,function(){
						//do nothing
						
					});
					
					$(selectedItems[dragindex]).animate({
						left:'+='+(withDifference*(-1)),
						top:'+='+(heightDifference*(-1)),
						width: dropsize.width,
						height: dropsize.height
					},redsettings.speed,function(){
						//Complete
						allowTodrag = true;
						if(redsettings.onafterswap!=null) redsettings.onafterswap(selectedItems[dragindex],selectedItems[dropindex]);
						
					});
					
					
				}
		
		
			});
			
			$(this).draggable({
			opacity: redsettings.opacity,
			cursor: 'move',
			zIndex: 8000,			
			helper: 'clone',
//			function() {
//			    var helperContainer = $('#divDragging');
//			    if (helperContainer!=null)
//			        return $(helperContainer.html())[0];
//			    else return $(this);			   
//			    
//			}, 
			containment: 'parent',
			scroll: false,			
			drag:function(event, ui){
			        if(!allowTodrag) return;
					dragindex=$(selectedItems).index(this);
				}
				
			});
			
		});
	};
	
});